package com.example.musicplayer.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;

public class ListenSoftInputView extends View {
    private static final int SOFT_KEYBOARD_HEIGHT = 200;
    private OnKeyboardStateChangeListener mListener;

    public ListenSoftInputView(Context context) {
        super(context);
    }

    public ListenSoftInputView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ListenSoftInputView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        if (Math.abs(h - oldh) >= SOFT_KEYBOARD_HEIGHT) {
            if (h > oldh) {
                if (mListener != null) {
                    mListener.onKeyboardStateChanged(OnKeyboardStateChangeListener.STATE_HIDE);
                }
            } else {
                if (mListener != null) {
                    mListener.onKeyboardStateChanged(OnKeyboardStateChangeListener.STATE_SHOW);
                }
            }
        }
    }

    public void setOnKeyboardStateChangeListener(OnKeyboardStateChangeListener listener) {
        mListener = listener;
    }

    public interface OnKeyboardStateChangeListener {
        public static int STATE_HIDE = 0;
        public static int STATE_SHOW = 1;

        void onKeyboardStateChanged(int state);
    }
}